package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduStudentMapper;
import com.zenith.edu.po.domain.EduStudent;
import com.zenith.edu.po.dto.EduStudentDTO;
import com.zenith.edu.po.vo.EduStudentVO;
import com.zenith.edu.service.EduStudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduStudentServiceImpl extends ServiceImpl<EduStudentMapper, EduStudent> implements EduStudentService {
    
    @Override
    public EduStudentVO createStudent(EduStudentDTO dto) {
        EduStudent student = new EduStudent();
        BeanUtils.copyProperties(dto, student);
        this.save(student);
        
        EduStudentVO vo = new EduStudentVO();
        BeanUtils.copyProperties(student, vo);
        return vo;
    }
    
    @Override
    public EduStudentVO getStudentById(Long id) {
        EduStudent student = this.getById(id);
        if (student == null) {
            return null;
        }
        
        EduStudentVO vo = new EduStudentVO();
        BeanUtils.copyProperties(student, vo);
        return vo;
    }
    
    @Override
    public EduStudentVO updateStudent(EduStudentDTO dto) {
        EduStudent student = new EduStudent();
        BeanUtils.copyProperties(dto, student);
        this.updateById(student);
        
        EduStudentVO vo = new EduStudentVO();
        BeanUtils.copyProperties(student, vo);
        return vo;
    }
    
    @Override
    public boolean deleteStudent(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduStudentVO> getStudentList() {
        List<EduStudent> studentList = this.list();
        return studentList.stream().map(student -> {
            EduStudentVO vo = new EduStudentVO();
            BeanUtils.copyProperties(student, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}